////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 1999-2010.
// -------------------------------------------------------------------------
//  File name:	AssetModelItem.h
//  Version:	v1.00
//  Created:	15/04/2009 by Paulo Zaffari
//  Description:	Header file for the class implementing IAssetDisplayItem
//								interface. It declares the headers of the actual used 
//								functions
// -------------------------------------------------------------------------  
//  History:
//		15/04/2009	11:00 - Paulo Zaffari - created
//		15/03/2010	19:51 - Nicusor Nedelcu - refactored
//
//////////////////////////////////////////////////////////////////////////// 

#ifndef __AssetModelItem_h__
#define __AssetModelItem_h__
#pragma once

#include "Asset Browser/AssetBrowserCommon.h"
#include <I3DEngine.h>
#include <IRenderAuxGeom.h>
#include <IShader.h>
#include "Util/GdiUtil.h"
#include "AssetBrowserPreviewModelDlg.h"

namespace AssetBrowser
{
	const float kDefaultModelRotationAngleX = 10.0f;
	const float kDefaultModelRotationAngleY = 70.0f;
};

class CAssetModelItem : public CAssetDisplay
{
	public:

		friend class CAssetBrowserPreviewModelDlg;

		static const int	kAssetDisplay_MaxThumbImageBufferSize = 2048*2048;

		enum EAssetModelItemDragCreationMode
		{
			eAssetModelItemDragCreationMode_AsBrush,
			eAssetModelItemDragCreationMode_AsGeomEntity
		};

		CAssetModelItem();
		~CAssetModelItem();

		bool										GetAssetFieldValue( const char* pFieldName, void* pDest );
		bool										Cache();
		bool										CacheFieldsInfo();
		bool										UnCache();
		bool										UnCacheThumbnail();
		void										OnBeginPreview( const HWND hQuickPreviewWnd, const HDC hMemDC );
		void										OnEndPreview();
		CDialog*								GetCustomPreviewPanel( CWnd* pParentWnd );
		void										InteractiveRender(	const HWND hRenderWindow, const CRect& rstViewport, int aMouseX, int aMouseY,
																								int aMouseDeltaX, int aMouseDeltaY, int aMouseWheelDelta, UINT aKeyFlags );
		void										OnInteractiveRenderKeyEvent( bool bKeyDown, UINT aChar, UINT aKeyFlags );
		bool										Render( const HWND hRenderWindow, const CRect& rstViewport, bool bCacheThumbnail = false );
		void*										CreateInstanceInViewport( float aX, float aY, float aZ );
		bool										MoveInstanceInViewport( const void* pDraggedObject, float aX, float aY, float aZ );
		void										AbortCreateInstanceInViewport( const void* pDraggedObject );
		bool										DrawThumbImage( const HDC hDC, const CRect& rRect );
		bool										SaveReportImage( const char *filePath ) const;
		bool										SaveReportText( const char *filePath ) const;
		void										CacheFieldsInfoForLoadedStatObj( IStatObj *pStatObj );
		void										CheckIfItsLod();
		void										ToXML(XmlNodeRef& node) const;
		void										FromXML(const XmlNodeRef& node);

		// from IUnknown - Inherited through IClassDesc.
		HRESULT STDMETHODCALLTYPE		QueryInterface( const IID &riid, void **ppvObj ); 
		ULONG STDMETHODCALLTYPE			AddRef();
		ULONG STDMETHODCALLTYPE			Release(); 

protected:

	void									SetCamera( CCamera &cam,const CRect &rcViewportRect );
	void									CalculateCameraPosition();
	void									DrawGrid();
	void									DrawTextOnReportImage( CAlphaBitmap &abm ) const;
	void									MakeLODsTrisString( CString& rOutValue ) const;
	void									MakeLODsVertsString( CString& rOutValue ) const;
	void									MakeLODsSubMeshesString( CString& rOutValue ) const;
	void									MakeLODsMeshSizeString( CString& rOutValue ) const;
	void									PrepareThumbTextInfo();

	Vec3									m_camTarget;
	float									m_camRadius, m_camZoom;
	CCamera								m_camera;
	AABB									m_aabb;
	Quat									m_crtRotation;
	UINT									m_lodCount, m_triangleCount, m_vertexCount, m_submeshCount;
	UINT									m_textureSize, m_nRef, m_physicsTriCount, m_physicsSize;
	bool									m_bSplitLODs;
	UINT									m_triCountLOD[MAX_STATOBJ_LODS_NUM];
	UINT									m_vertCountLOD[MAX_STATOBJ_LODS_NUM];
	UINT									m_subMeshCountLOD[MAX_STATOBJ_LODS_NUM];
	UINT									m_meshSizeLOD[MAX_STATOBJ_LODS_NUM];
	IStatObj*							m_pObject;
	IRenderer*						m_pRenderer;
	bool									m_bGrid;
	bool									m_bAxis;
	std::vector<CDLight>	m_lights;
	ColorF								m_clearColor;
	bool									m_bRotate, m_bIsLod;
	float									m_rotateAngle;
	bool									m_bShowObject, m_bWireframe;
	float									m_fov, m_rotationX, m_rotationY, m_translateX, m_translateY;
	CAlphaBitmap					m_oCachedBmp;
	static UINT						m_frameBufferScreenshot[kAssetDisplay_MaxThumbImageBufferSize];
	static CAssetBrowserPreviewModelDlg	s_modelPreviewDlg;
	EAssetModelItemDragCreationMode m_dragCreationMode;
};

#endif //__AssetModelItem_h__
